package org.example.everyday;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/5/4 0004 18:46
 */
public class Lc1823 {
    public static void main(String[] args) {
        Lc1823 lc1823 = new Lc1823();
        int res = lc1823.findTheWinner(5,2);
        System.out.println(res);
    }

    /**
     * 方法描述: 约瑟夫环的解法
     *
     * @param n .
     * @param k .
     * @return int
     * @author 安路路
     * @date 2022/5/4 0004  18:47
     */
    public int findTheWinner(int n, int k) {
        Queue<Integer> queue = new ArrayDeque<Integer>();
        for (int i = 1; i <=n ; i++) {
            queue.offer(i);
        }

        while (queue.size() > 1){
            //重新压入队列中
            for (int i = 1; i < k; i++) {
                queue.offer(queue.poll());
            }
            queue.poll();
        }
        return queue.peek();
    }
}
